//
//  HerosNextJiqiaoTableViewController.m
//  LOLBOX
//
//  Created by lanou3g on 15/12/17.
//  Copyright © 2015年 张明. All rights reserved.
//

#import "HerosNextJiqiaoTableViewController.h"
#import "HeroHelper.h"
#import "HerosJiNengTableViewCell.h"
#import "HerosPifuModelHelper.h"
#import "KVNProgressConfiguration.h"
#import "KVNProgress.h"
#import "UIImageView+WebCache.h"
#import "HerosContentTableViewCell.h"
#import "HerosDetailsModel.h"
#import "HerosModelHelper.h"
static CGFloat kImageHeight = 200.f;
@interface HerosNextJiqiaoTableViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *nameLable;
@property(nonatomic,strong)UILabel *typeLable;
@property(nonatomic,strong)UILabel *priceLable;
@property(nonatomic,assign)CGFloat height;
@property(nonatomic,strong)NSMutableArray *allDataArray;
@property(nonatomic,strong)NSString *jiNengString;
//小菊花
@property(nonatomic,strong)KVNProgressConfiguration *kVn;
@end
@implementation HerosNextJiqiaoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allDataArray = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    self.kVn.fullScreen = YES;
    [KVNProgress showWithStatus:@"玩命加载中..."];
    [self createImageViewChild];
    self.tableView.frame = CGRectMake(0, 40, self.tableView.frame.size.width, self.tableView.frame.size.height -20);
    self.tableView.backgroundColor = [UIColor whiteColor];

    [self.tableView registerClass:[HerosContentTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[HerosJiNengTableViewCell class] forCellReuseIdentifier:@"celltwo"];
    HerosPifuModelHelper *helper = [HerosPifuModelHelper defaultHerosPifuHelper];
    helper.price = self.price;
    NSLog(@"1111----%@",self.aString);
//    数据请求
    [[HeroHelper shareHeroWithHelpData]getHeroDataWithUrl2:[NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiHeroDetail.php?OSType=iOS8.3&heroName=%@&v=108",self.aString] Key:nil successData:^(id responseObj) {
        NSDictionary *dic = responseObj;
                 [self jiexiData:dic];
        [KVNProgress dismiss];
    } fail:^(NSError *err) {
        
    }];
    self.tableView.contentInset = UIEdgeInsetsMake(kImageHeight, 0, 0, 0);
    self.imageView = [[UIImageView alloc] init];
    self.imageView.frame = CGRectMake(0, -kImageHeight , self.tableView.frame.size.width, kImageHeight);
    [self createImageViewChild];
    
    [self.view addSubview:self.imageView];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark --- 数据解析
- (void)jiexiData:(NSDictionary *)dic {
    HerosDetailsModel *model = [[HerosDetailsModel alloc] init];
    [model setValuesForKeysWithDictionary:dic];
    model.story = dic[@"description"];
    NSString *string1 = [NSString stringWithFormat:@"%@_B",dic[@"name"]];
    NSString *string2 = [NSString stringWithFormat:@"%@_Q",dic[@"name"]];
    NSString *string3 = [NSString stringWithFormat:@"%@_W",dic[@"name"]];
    NSString *string4 = [NSString stringWithFormat:@"%@_E",dic[@"name"]];
    NSString *string5 = [NSString stringWithFormat:@"%@_R",dic[@"name"]];
    [self.allDataArray addObject:dic[string1]];
    [self.allDataArray addObject:dic[string2]];
    [self.allDataArray addObject:dic[string3]];
    [self.allDataArray addObject:dic[string4]];
    [self.allDataArray addObject:dic[string5]];
    
    model.likeArray = [NSMutableArray array];
    model.hateArray = [NSMutableArray array];
    
    [model.likeArray addObjectsFromArray:dic[@"like"]];
    [model.hateArray addObjectsFromArray:dic[@"hate"]];
    
    model.nextName = self.allDataArray[0][@"name"];
    model.cost = self.allDataArray[0][@"cost"];
    model.cooldown = self.allDataArray[0][@"cooldown"];
    model.description1 = self.allDataArray[0][@"description"];
    model.effect = self.allDataArray[0][@"effect"];
    HerosPifuModelHelper *helper = [HerosPifuModelHelper defaultHerosPifuHelper];
    helper.price = self.price;
    helper.type = model.tags;
   [[HeroHelper shareHeroWithHelpData]getHeroDataWithUrl2:[NSString stringWithFormat:@"http://box.dwstatic.com/apiHeroSkin.php?hero=%@&v=108&OSType=iOS8.3&versionName=2.2.5",self.aString] Key:nil successData:^(id responseObj) {
       NSArray *array = responseObj;
       [helper.nameArray removeAllObjects];
       [helper.imageArray removeAllObjects];
       for (NSDictionary *dic in array) {
           [helper.nameArray addObject:dic[@"name"]];
           [helper.imageArray addObject:dic[@"bigImg"]];
       }
       self.typeLable.text = [HerosPifuModelHelper defaultHerosPifuHelper].type;
       NSArray *array1 = [[HerosPifuModelHelper defaultHerosPifuHelper].price componentsSeparatedByString:@","];
       self.priceLable.text = [NSString stringWithFormat:@"金币:%@ 点卷:%@",array1[0],array1[1]];
       
       self.nameLable.text = [HerosPifuModelHelper defaultHerosPifuHelper].nameArray[0];
       NSString *string = [HerosPifuModelHelper defaultHerosPifuHelper].imageArray[0];
       [self.imageView sd_setImageWithURL:[NSURL URLWithString:string]];
       
       
   } fail:^(NSError *err) {
       
   }];

    [HerosModelHelper defaultHerosModelHelper].model = model;
    [self.tableView reloadData];
//    NSLog(@"- - array--%@",self.allDataArray);
}

#pragma mark - 添加控件
-(void)createImageViewChild{
    self.nameLable = [[UILabel alloc] init];
    self.nameLable.frame = CGRectMake(10, self.imageView.frame.size.height -80, self.view.frame.size.width-20, 20);
    // self.nameLable.backgroundColor = [UIColor redColor];
    self.nameLable.font = [UIFont systemFontOfSize:14];
    self.nameLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.nameLable];
    
    self.typeLable = [[UILabel alloc] init];
    self.typeLable.frame = CGRectMake(10, self.imageView.frame.size.height -60, self.view.frame.size.width - 20, 20);
    // self.typeLable.backgroundColor = [UIColor blueColor];
    self.typeLable.font = [UIFont systemFontOfSize:14];
    self.typeLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.typeLable];
    
    self.priceLable = [[UILabel alloc] init];
    self.priceLable.frame = CGRectMake(10, self.imageView.frame.size.height -40, self.view.frame.size.width - 20, 20);
    //self.priceLable.backgroundColor = [UIColor greenColor];
    self.priceLable.font = [UIFont systemFontOfSize:14];
    self.priceLable.textColor = [UIColor whiteColor];
    [self.imageView addSubview:self.priceLable];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HerosDetailsModel *model = [HerosModelHelper defaultHerosModelHelper].model;
    if (indexPath.section == 1) {
        HerosContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.contentLable.text = model.tips;
        self.height = [cell getHeightWithString:model.tips];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.section == 2) {
        HerosContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.contentLable.text = model.opponentTips;
        self.height = [cell getHeightWithString:model.opponentTips];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        HerosJiNengTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"celltwo" forIndexPath:indexPath];
        [cell.firstButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/abilities/%@_B_64x64.png?v=10&OSType=iOS7.0.3",model.name]]];
        [cell.secondButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/abilities/%@_Q_64x64.png?v=10&OSType=iOS7.0.3",model.name]]];
        [cell.thirdButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/abilities/%@_W_64x64.png?v=10&OSType=iOS7.0.3",model.name]]];
        [cell.fourthButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/abilities/%@_E_64x64.png?v=10&OSType=iOS7.0.3",model.name]]];
        [cell.fifthButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://img.lolbox.duowan.com/abilities/%@_R_64x64.png?v=10&OSType=iOS7.0.3",model.name]]];
        
        UITapGestureRecognizer *firstTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstAction:)];
        UITapGestureRecognizer *secondTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(secondAction:)];
        UITapGestureRecognizer *thirdTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thirdAction:)];
        UITapGestureRecognizer *fourthTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fourthAction:)];
        UITapGestureRecognizer *fifthTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fifthAction:)];
        
        [cell.firstButton addGestureRecognizer:firstTap];
        [cell.secondButton addGestureRecognizer:secondTap];
        [cell.thirdButton addGestureRecognizer:thirdTap];
        [cell.fourthButton addGestureRecognizer:fourthTap];
        [cell.fifthButton addGestureRecognizer:fifthTap];
        
        cell.firstButton.userInteractionEnabled = YES;
        cell.secondButton.userInteractionEnabled = YES;
        cell.thirdButton.userInteractionEnabled = YES;
        cell.fourthButton.userInteractionEnabled = YES;
        cell.fifthButton.userInteractionEnabled = YES;
        
        if (([model.cost isEqualToString:@""] || model.cost == nil)&&([model.cooldown isEqualToString:@""]||model.cooldown == nil)) {
            if (([model.description1 isEqualToString:@""]|| model.description1 == nil)) {
                self.jiNengString = [NSString stringWithFormat:@"%@\n\n%@",model.nextName,model.effect];
            }else if (([model.effect isEqualToString:@""]|| model.effect == nil)) {
                self.jiNengString = [NSString stringWithFormat:@"%@\n\n%@",model.nextName,model.description1];
            }else{
                self.jiNengString = [NSString stringWithFormat:@"%@\n\n%@\n\n%@",model.nextName,model.description1,model.effect];
            }
        }else if([model.cost isEqualToString:@""] && ![model.cooldown isEqualToString:@""]){
            self.jiNengString = [NSString stringWithFormat:@"%@\n\n%@\n\n%@\n\n冷却时间:%@",model.nextName,model.description1,model.effect,model.cooldown];
        }else if (![model.cost isEqualToString:@""] &&[model.cooldown isEqualToString:@""]){
            self.jiNengString = [NSString stringWithFormat:@"%@\n\n%@\n\n%@\n\n法力消耗:%@",model.nextName,model.description1,model.effect,model.cost];
        }else if (![model.cost isEqualToString:@""]&& ![model.cooldown isEqualToString:@""]){
            self.jiNengString = [NSString stringWithFormat:@"%@\n\n%@\n\n%@\n\n法力消耗:%@\n冷却时间:%@",model.nextName,model.description1,model.effect,model.cost,model.cooldown];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.detaliLable.text = self.jiNengString;
        self.height = [cell getHetghtWithJiNeng:self.jiNengString];
        self.height += 40;
        return cell;
    }
    return nil;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return self.height +10;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    lable.font = [UIFont systemFontOfSize:18];
    lable.textColor = [UIColor redColor];
    if (section == 0) {
        lable.text = @"  技能介绍";
        return lable;
    }else if (section == 1){
        lable.text = @"  使用技巧";
        return lable;
    }else{
        lable.text = @"  对抗技巧";
        return lable;
    }
}


#pragma mark - - -- - - - -- - - - - - - - --  -- - - - - - -- action
-(void)firstAction:(UITapGestureRecognizer *)sender{
    
    HerosDetailsModel *model = [HerosModelHelper defaultHerosModelHelper].model;
    model.nextName = self.allDataArray[0][@"name"];
    model.cost = self.allDataArray[0][@"cost"];
    model.cooldown = self.allDataArray[0][@"cooldown"];
    model.description1 = self.allDataArray[0][@"description"];
    model.effect = self.allDataArray[0][@"effect"];
    //[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:(UITableViewRowAnimationBottom)];
    [self.tableView reloadData];
    
}

-(void)secondAction:(UITapGestureRecognizer *)sender{
    
    
    HerosDetailsModel *model = [HerosModelHelper defaultHerosModelHelper].model;
    model.nextName = self.allDataArray[1][@"name"];
    model.cost = self.allDataArray[1][@"cost"];
    model.cooldown = self.allDataArray[1][@"cooldown"];
    model.description1 = self.allDataArray[1][@"description"];
    model.effect = self.allDataArray[1][@"effect"];
    //[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:(UITableViewRowAnimationBottom)];
    
    [self.tableView reloadData];
}

-(void)thirdAction:(UITapGestureRecognizer *)sender{
    
    HerosDetailsModel *model = [HerosModelHelper defaultHerosModelHelper].model;
    model.nextName = self.allDataArray[2][@"name"];
    model.cost = self.allDataArray[2][@"cost"];
    model.cooldown = self.allDataArray[2][@"cooldown"];
    model.description1 = self.allDataArray[2][@"description"];
    model.effect = self.allDataArray[2][@"effect"];
    // [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:(UITableViewRowAnimationBottom)];
    [self.tableView reloadData];
}

-(void)fourthAction:(UITapGestureRecognizer *)sender{
    
    HerosDetailsModel *model = [HerosModelHelper defaultHerosModelHelper].model;
    model.nextName = self.allDataArray[3][@"name"];
    model.cost = self.allDataArray[3][@"cost"];
    model.cooldown = self.allDataArray[3][@"cooldown"];
    model.description1 = self.allDataArray[3][@"description"];
    model.effect = self.allDataArray[3][@"effect"];
    // [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:(UITableViewRowAnimationBottom)];
    [self.tableView reloadData];
    
}

-(void)fifthAction:(UITapGestureRecognizer *)sender{
    
    HerosDetailsModel *model = [HerosModelHelper defaultHerosModelHelper].model;
    model.nextName = self.allDataArray[4][@"name"];
    model.cost = self.allDataArray[4][@"cost"];
    model.cooldown = self.allDataArray[4][@"cooldown"];
    model.description1 = self.allDataArray[4][@"description"];
    model.effect = self.allDataArray[4][@"effect"];
    // [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:(UITableViewRowAnimationBottom)];
    [self.tableView reloadData];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset  = scrollView.contentOffset.y;
    if (yOffset < -kImageHeight) {
        CGRect f = self.imageView.frame;
        f.origin.y = yOffset;
        f.size.height =  -yOffset;
        self.imageView.frame = f;
    }
    self.nameLable.frame = CGRectMake(10, self.imageView.frame.size.height -80, self.view.frame.size.width-20, 20);
    self.typeLable.frame = CGRectMake(10, self.imageView.frame.size.height -60, self.view.frame.size.width - 20, 20);
    self.priceLable.frame = CGRectMake(10, self.imageView.frame.size.height -40, self.view.frame.size.width - 20, 20);
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
